package com.hzdl.library.dao.impl;

import com.hzdl.library.dao.LibraryBookDao;
import com.hzdl.library.domain.BookInfo;
import com.hzdl.library.util.JDBCUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

//孙彬棋
public class LibraryBookDaoImpl implements LibraryBookDao {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    //    添加书籍
    @Override
    public void addBook(BookInfo bookInfo) {
        String sql = "insert into book_info values(null,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql, bookInfo.getName(), bookInfo.getAuthor(), bookInfo.getPublish(),
                bookInfo.getISBN(), bookInfo.getIntroduction(), bookInfo.getLanguage(), bookInfo.getPrice(),
                bookInfo.getPubdate(), bookInfo.getClass_id(), bookInfo.getPressmark(), bookInfo.getState());
    }

    //    删除书籍
    @Override
    public void deleteBook(String bookId) {
        String sql = "delete from book_info where book_id = ?";
        jdbcTemplate.update(sql, bookId);
    }

    //    修改书籍信息

    @Override
    public void updateBook(BookInfo bookInfo) {
        String sql = "update book_info set name = ? , author = ? , publish = ? , ISBN = ? , introduction = ? , language = ? , price = ? , pubdate = ? , class_id = ? , pressmark = ? , state = ? where book_id = ?";
        jdbcTemplate.update(sql, bookInfo.getName(), bookInfo.getAuthor(), bookInfo.getPublish(),
                bookInfo.getISBN(), bookInfo.getIntroduction(), bookInfo.getLanguage(), bookInfo.getPrice(),
                bookInfo.getPubdate(), bookInfo.getClass_id(), bookInfo.getPressmark(), bookInfo.getState(),
                bookInfo.getBook_id());
    }

    //    查询出有多少本书
    @Override
    public Integer findCountByBid(String book_id, String searchBook) {
        String sql = null;
        Integer integer = null;
        if(StringUtils.isEmpty(book_id)){
            sql = "select count(*) from book_info where name like concat('%',?,'%')";
            integer = jdbcTemplate.queryForObject(sql,Integer.class,searchBook);
        }else{
            sql = "select count(*) from book_info where book_id = ? and name like concat('%',?,'%')";
            integer = jdbcTemplate.queryForObject(sql,Integer.class,book_id,searchBook);
        }
        return integer;
    }
//      模糊查询
    @Override
    public List<BookInfo> findPageByBid(String book_id, Integer index, Integer rowsInteger, String searchBook) {
        String sql = null;
        List<BookInfo> list = null;
        if(StringUtils.isEmpty(book_id)){
            sql = "select * from book_info where name like concat('%',?,'%') order by book_id desc limit ?,?";
            list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<BookInfo>(BookInfo.class),searchBook,index,rowsInteger);
        }else{
            sql = "select * from book_info where book_id = ? and name like concat('%',?,'%') order by book_id desc limit ?,?";
            list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<BookInfo>(BookInfo.class),book_id,searchBook,index,rowsInteger);
        }
        return list;
    }
//      书籍详情
    @Override
    public BookInfo findDetail(String book_id) {
        String sql = "select * from book_info where book_id = ?";
        BookInfo bookInfo = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<BookInfo>(BookInfo.class), book_id);
        return bookInfo;
    }

    @Test
    public void method() {
//        deleteBook("1");
    }
}
